package member

import (
	"fmt"
	"gpt-api/framework/codes"
	"gpt-api/framework/ginplus"
	"gpt-api/pkg/model"
	"gpt-api/pkg/response"

	"github.com/gin-gonic/gin"
)

// 添加图片
func (h *Handler) AddMemberImage(c *gin.Context) error {
	// 验证参数
	userID := ginplus.GetUserID(c)
	var req struct {
		ImageUrl []string `json:"image_url" validate:"required"`
		MemberID uint64   `json:"member_id" validate:"required,gt=0"`
		Type     int8     `json:"type" validate:"required,gt=0"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	if len(req.ImageUrl) == 0 {
		return ginplus.ReplyError(c, codes.InvalidParams, nil)
	}

	// 验证成员
	member, err := (&model.Member{}).GetMemberById(h.db, req.MemberID)
	if err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	if member.UserID != uint(userID) {
		return ginplus.ReplyError(c, codes.MemberNotFound, err)
	}

	// 判断图片最多9张
	if len(req.ImageUrl) > 9 {
		return ginplus.ReplyError(c, codes.ImagesMaxLimitTo9, nil)
	}

	// 批量添加图片
	var ids []uint64
	if len(req.ImageUrl) > 0 {
		for _, imageUrl := range req.ImageUrl {
			memberImage := model.MemberImage{
				ImageUrl: imageUrl,
				UserID:   uint64(userID),
				MemberID: uint(req.MemberID),
				Type:     req.Type,
			}

			id, err := memberImage.Create(h.db)
			if err != nil {
				return ginplus.ReplyInternalError(c, err)
			}
			ids = append(ids, id)
		}
	}

	return ginplus.ReplyJSON(c, ids)
}

// 获取图片列表
func (h *Handler) GetMemberImages(c *gin.Context) error {
	// 验证参数
	userID := ginplus.GetUserID(c)
	var req struct {
		MemberID uint64 `form:"id" validate:"gte=0"`
		Type     int8   `form:"type" validate:"gte=0"`
		Page     int    `form:"page" default:"1"`
		Size     int    `form:"size" default:"10"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 验证成员
	if req.MemberID > 0 {
		member, err := (&model.Member{}).GetMemberById(h.db, req.MemberID)
		if err != nil {
			return ginplus.ReplyBadRequest(c, err)
		}
		if member.UserID != uint(userID) {
			return ginplus.ReplyError(c, codes.MemberNotFound, err)
		}
	}

	// 查询图片
	images, err := (&model.MemberImage{}).GetMemberImages(h.db, req.MemberID, uint8(req.Type), req.Page, req.Size)
	if err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 整理返回数据
	var listResp []response.MemberImageListResp
	for _, image := range images {
		listResp = append(listResp, response.MemberImageListResp{
			ID:       image.ID,
			ImageUrl: image.ImageUrl,
			Type:     image.Type,
		})
	}

	// 统计图片
	images_count, err := (&model.MemberImage{}).CountMemberImages(h.db, req.MemberID)
	if err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	resp := &response.MemberImageResp{
		List:  listResp,
		Total: images_count,
	}

	return ginplus.ReplyJSON(c, resp)
}

// 批量删除图片
func (h *Handler) DeleteMemberImage(c *gin.Context) error {
	// 验证参数
	userID := ginplus.GetUserID(c)
	var req struct {
		ImageIDs []uint64 `json:"id" validate:"required"`
	}
	if err := c.Bind(&req); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	fmt.Println(req.ImageIDs)

	// 查询图片信息
	images, err := (&model.MemberImage{}).GetMemberImagesByIds(h.db, req.ImageIDs)
	if err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	if len(images) == 0 {
		return ginplus.ReplyError(c, codes.MemberImagesNotFound, err)
	}

	// 根据ids查询成员信息
	var memberIds []uint64
	for _, image := range images {
		memberIds = append(memberIds, uint64(image.MemberID))
	}
	members, err := (&model.Member{}).GetMembersByIds(h.db, memberIds)
	if err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}

	// 验证成员信息
	if len(members) == 0 {
		return ginplus.ReplyError(c, codes.MemberImagesNotFound, err)
	}
	for _, member := range members {
		if member.UserID != uint(userID) {
			return ginplus.ReplyError(c, codes.MemberImagesNotFound, err)
		}
	}

	// 删除图片
	if err := (&model.MemberImage{}).DeleteMemberImages(h.db, req.ImageIDs); err != nil {
		return ginplus.ReplyBadRequest(c, err)
	}
	return ginplus.ReplyJSON(c, nil)
}
